<template>
  <div>
    <div class="detail_box">
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>设备名称：</label>
            <el-input type="text" placeholder="请输入设备名称" v-model="keyword"></el-input>
          </div>
          <div class="operate_item">
            <span class="operate_btn search_btn" @click="getTableInfo(1)">查询</span>
            <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          </div>
        </div>
        <div class="btn_box">
          <span class="operate_btn search_btn" @click="synData">同步数据</span>
        </div>
      </div>
      <div class="table_box">
        <el-table
          border
          v-loading="loading"
          :header-cell-style="tableHeader"
          ref="multipleTable"
          :data="tableInfo"
          tooltip-effect="dark"
          style="width: 100%;">
          <el-table-column
            width="160"
            sortable
            prop="modifytime"
            label="时间">
            <template slot-scope="scope">
              <span>{{ formatTime(scope.row.modifytime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            width="80"
            prop="recordcode"
            label="ID">
          </el-table-column>
          <el-table-column
            prop="mainframecode"
            label="网关编码">
          </el-table-column>
          <el-table-column
            prop="mainframename"
            label="网关名称">
          </el-table-column>
          <el-table-column
            width="170"
            label="网关所在楼宇ID"
            prop="buildingid">
          </el-table-column>
          <el-table-column
            width="170"
            label="经度"
            prop="gpsx3d">
          </el-table-column>
          <el-table-column
            width="170"
            label="纬度"
            prop="gpsy3d">
          </el-table-column>
          <el-table-column
            label="操作"
            v-if="btn&&(btn.bindMonitor||btn.bindHwy)"
            fixed="right"
            width="200">
            <template slot-scope="scope">
              <div class="table_btn">
                <span class="change_info" @click="openPopup(scope.row,1)" v-if="btn.bindMonitor">绑定监控</span>
                <span class="change_info" @click="openPopup(scope.row,2)" v-if="btn.bindHwy">绑定好望云设备</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="record_page">
        <span class="page_count">共<i>{{ totals }}</i>条</span>
        <el-pagination
          @size-change="sizeChange"
          @current-change="getTableInfo"
          background
          :current-page="currentPage"
          :page-sizes="[15, 20, 30, 40]"
          :page-size="pageSize"
          layout="prev, pager, next, sizes,jumper"
          :total="totals">
        </el-pagination>
      </div>
    </div>
    <el-dialog :title="deviceName+(bindType===1?'绑定监控设备':'绑定好望云设备')" :visible.sync="isBind" width="80%"
               @close='isBind=false' append-to-body v-dialogDrag>
      <bindMonitorDevice :deviceId="deviceId" v-if="isBind&&bindType===1"></bindMonitorDevice>
      <bindHwyDevice :deviceId="deviceId" v-if="isBind&&bindType===2"></bindHwyDevice>
    </el-dialog>
  </div>
</template>

<script>

import bindMonitorDevice from "./bindMonitorDevice";
import bindHwyDevice from "./bindHwyDevice";
import commons from "@/utils/common";
import {getBtn} from "@/utils/btnAuth";
import {getFire, synFireMainframe} from "../../../api/mainframe";

export default {
  components: {
    bindMonitorDevice,
    bindHwyDevice
  },
  data() {
    return {
      keyword: '',
      tableInfo: [],
      currentPage: 1,
      pageSize: 15,
      totals: 0,
      loading: false,
      isBind: false,
      deviceName: '',
      deviceId: '',
      bindType: '',
      btn: {}
    }
  },

  mounted() {
    getBtn().then(res => {
      this.btn = res;
      if (this.btn.query) {
        this.getTableInfo(1);
      }
    })
  },

  methods: {
    // 获取列表信息
    getTableInfo(page) {
      let that = this;
      that.loading = true;
      that.currentPage = page;
      let queryParams = {
        limit: that.pageSize,
        page: page,
        name: that.keyword
      }
      getFire(queryParams).then(res => {
        that.tableInfo = res.data ? res.data.list : [];
        that.totals = res.data ? res.data.count : 0;
        that.loading = false;
      }, (err) => {
        that.loading = false;
      });
    },

    resetTable() {
      this.pageSize = 15;
      this.keyword = '';
      this.getTableInfo(1);
    },

    synData() {
      let that = this;
      that.$confirm('确定同步数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.loading = true;
        synFireMainframe().then(res => {
          if (res.success) {
            that.getTableInfo(that.currentPage);
            that.$message.success('同步成功！');
          }
          that.loading = false;
        }, (err) => {
          that.loading = false;
          that.$message.error('同步失败，请重试');
        });
      }).catch(() => {
        // console.log('fail')
      });
    },

    openPopup(obj, type) {
      this.isBind = true;
      this.deviceId = obj.recordcode;
      this.deviceName = obj.mainframename;
      this.bindType = type;
    },


    sizeChange(val) {
      this.pageSize = val;
      this.getTableInfo(1)
    },


    formatTime(time) {
      return commons.formatTime(time);
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
    },
  }
}
</script>

<style scoped>

</style>
